﻿namespace PLCComHelperProj
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;

    public class TagGroup
    {
        private byte[] byte_0;
        private Dictionary<int, byte> dictionary_0 = new Dictionary<int, byte>(0x89);
        private Dictionary<string, Tag> dictionary_1 = new Dictionary<string, Tag>(0x89);
        private int int_0;
        private int int_1;
        private string m_Name;
        private int readCount;
        private string string_0;

        public TagGroup(string Name, string MMType, int Block, int Address, int ReadCount)
        {
            this.m_Name = Name;
            this.string_0 = MMType;
            this.Block = Block;
            this.int_1 = Address;
            this.readCount = ReadCount;
            this.byte_0 = new byte[ReadCount];
        }

        public e_PLC_MMType get_MMtype(out string funCode)
        {
            e_PLC_MMType type = e_PLC_MMType.MM_UNKNOW;
            funCode = FuckProtect.DataFrom(0x516);
            string str = this.string_0;
            if (str == FuckProtect.DataFrom(0x6ec))
            {
                type = e_PLC_MMType.MM_I_E;
                funCode = FuckProtect.DataFrom(0x75e);
                return type;
            }
            if (str == FuckProtect.DataFrom(0x6f2))
            {
                type = e_PLC_MMType.MM_I_E;
                funCode = FuckProtect.DataFrom(0x75e);
                return type;
            }
            if (str == FuckProtect.DataFrom(0x6f8))
            {
                type = e_PLC_MMType.MM_A_Q;
                funCode = FuckProtect.DataFrom(0x766);
                return type;
            }
            if (str == FuckProtect.DataFrom(0x6fe))
            {
                type = e_PLC_MMType.MM_A_Q;
                funCode = FuckProtect.DataFrom(0x766);
                return type;
            }
            if (str == FuckProtect.DataFrom(0x70a))
            {
                type = e_PLC_MMType.MM_M_F;
                funCode = FuckProtect.DataFrom(0x76e);
                return type;
            }
            if (str == FuckProtect.DataFrom(0x718))
            {
                type = e_PLC_MMType.MM_PI_PE;
                funCode = FuckProtect.DataFrom(0x776);
                return type;
            }
            if (str == FuckProtect.DataFrom(0x728))
            {
                type = e_PLC_MMType.MM_PQ_PA;
                funCode = FuckProtect.DataFrom(0x77e);
                return type;
            }
            if (str == FuckProtect.DataFrom(0x730))
            {
                type = e_PLC_MMType.MM_DB;
                funCode = FuckProtect.DataFrom(0x786);
                return type;
            }
            if (str == FuckProtect.DataFrom(0x738))
            {
                type = e_PLC_MMType.MM_V;
                funCode = FuckProtect.DataFrom(0x786);
                return type;
            }
            if (str == FuckProtect.DataFrom(0x746))
            {
                type = e_PLC_MMType.MM_AI_AE;
                funCode = FuckProtect.DataFrom(0x544);
                return type;
            }
            if (str == FuckProtect.DataFrom(0x756))
            {
                type = e_PLC_MMType.MM_AQ_AA;
                funCode = FuckProtect.DataFrom(0x78e);
                return type;
            }
            return type;
        }

        public e_PLC_MMType Get_MMtype()
        {
            string funCode = "";
            return this.get_MMtype(out funCode);
        }

        public int GetBitData(int address, int bitIndex)
        {
            if (this.dictionary_0.ContainsKey(address))
            {
                byte num = this.dictionary_0[address];
                return this.method_2(num, bitIndex);
            }
            return -9999;
        }

        public int GetByteData(int address)
        {
            if (this.dictionary_0.ContainsKey(address))
            {
                return this.dictionary_0[address];
            }
            return -9999;
        }

        public float GetFloatData(int address)
        {
            if (this.dictionary_0.ContainsKey(address))
            {
                byte[] buffer = new byte[] { this.dictionary_0[address + 3], this.dictionary_0[address + 2], this.dictionary_0[address + 1], this.dictionary_0[address] };
                return BitConverter.ToSingle(buffer, 0);
            }
            return -9999f;
        }

        public int GetIntData(int address)
        {
            if (this.dictionary_0.ContainsKey(address))
            {
                byte[] buffer = new byte[] { this.dictionary_0[address + 3], this.dictionary_0[address + 2], this.dictionary_0[address + 1], this.dictionary_0[address] };
                return BitConverter.ToInt32(buffer, 0);
            }
            return -9999;
        }

        public short GetShortValue(int address)
        {
            if (this.dictionary_0.ContainsKey(address))
            {
                byte[] buffer = new byte[] { this.dictionary_0[address + 1], this.dictionary_0[address] };
                return BitConverter.ToInt16(buffer, 0);
            }
            return -9999;
        }

        private int method_2(byte byte_1, int int_3)
        {
            int num = byte_1 & Convert.ToInt32(Math.Pow(2.0, (double) int_3));
            if (num <= 0)
            {
                return 0;
            }
            return 1;
        }

        public void SetBuff(byte[] buff, int soureIndex)
        {
            Array.Copy(this.byte_0, 0, buff, soureIndex, this.ReadCount);
        }

        public void SetData(int index, byte bytes)
        {
            this.dictionary_0[this.StartAddress + index] = bytes;
        }

        public int Block
        {
            get
            {
                return this.int_0;
            }
            set
            {
                this.int_0 = value;
            }
        }

        public string MMType
        {
            get
            {
                return this.string_0;
            }
            set
            {
                this.string_0 = value.ToUpper();
            }
        }

        public string Name
        {
            get
            {
                return this.m_Name;
            }
            set
            {
                this.m_Name = value;
            }
        }

        public int ReadCount
        {
            get
            {
                return this.readCount;
            }
            set
            {
                this.readCount = value;
            }
        }

        public int StartAddress
        {
            get
            {
                return this.int_1;
            }
            set
            {
                this.int_1 = value;
            }
        }

        public Dictionary<string, Tag> Tags
        {
            get
            {
                return this.dictionary_1;
            }
            set
            {
                this.dictionary_1 = value;
            }
        }
    }
}

